/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO.Financial;

import DAO.exceptions.NonexistentEntityException;
import DAO.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import FinancialEntity.Date;
import FinancialEntity.Quote;
import FinancialEntity.QuotePK;
import FinancialEntity.Stock;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Sebastian
 */
public class QuoteDAO implements Serializable, DAOFinancial {
    
    public void create(Quote quote, EntityManager em) throws PreexistingEntityException, Exception {
        if (quote.getQuotePK() == null) {
            quote.setQuotePK(new QuotePK());
        }
        quote.getQuotePK().setStockid(quote.getStock().getStockPK().getId());
        try {
            Date dateid = quote.getDateid();
            if (dateid != null) {
                dateid = em.getReference(dateid.getClass(), dateid.getId());
                quote.setDateid(dateid);
            }
            Stock stock = quote.getStock();
            if (stock != null) {
                stock = em.getReference(stock.getClass(), stock.getStockPK());
                quote.setStock(stock);
            }
            em.persist(quote);
            if (dateid != null) {
                dateid.getQuoteList().add(quote);
                dateid = em.merge(dateid);
            }
            if (stock != null) {
                stock.getQuoteList().add(quote);
                stock = em.merge(stock);
            }
        } catch (Exception ex) {
            if (findQuote(quote.getQuotePK(), em) != null) {
                throw new PreexistingEntityException("Quote " + quote + " already exists.", ex);
            }
            throw ex;
        } 
    }

    public void edit(Quote quote, EntityManager em) throws NonexistentEntityException, Exception {
        quote.getQuotePK().setStockid(quote.getStock().getStockPK().getId());
        try {
            Quote persistentQuote = em.find(Quote.class, quote.getQuotePK());
            Date dateidOld = persistentQuote.getDateid();
            Date dateidNew = quote.getDateid();
            Stock stockOld = persistentQuote.getStock();
            Stock stockNew = quote.getStock();
            if (dateidNew != null) {
                dateidNew = em.getReference(dateidNew.getClass(), dateidNew.getId());
                quote.setDateid(dateidNew);
            }
            if (stockNew != null) {
                stockNew = em.getReference(stockNew.getClass(), stockNew.getStockPK());
                quote.setStock(stockNew);
            }
            quote = em.merge(quote);
            if (dateidOld != null && !dateidOld.equals(dateidNew)) {
                dateidOld.getQuoteList().remove(quote);
                dateidOld = em.merge(dateidOld);
            }
            if (dateidNew != null && !dateidNew.equals(dateidOld)) {
                dateidNew.getQuoteList().add(quote);
                dateidNew = em.merge(dateidNew);
            }
            if (stockOld != null && !stockOld.equals(stockNew)) {
                stockOld.getQuoteList().remove(quote);
                stockOld = em.merge(stockOld);
            }
            if (stockNew != null && !stockNew.equals(stockOld)) {
                stockNew.getQuoteList().add(quote);
                stockNew = em.merge(stockNew);
            }
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                QuotePK id = quote.getQuotePK();
                if (findQuote(id, em) == null) {
                    throw new NonexistentEntityException("The quote with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } 
    }

    public void destroy(QuotePK id, EntityManager em) throws NonexistentEntityException {
            Quote quote;
            try {
                quote = em.getReference(Quote.class, id);
                quote.getQuotePK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The quote with id " + id + " no longer exists.", enfe);
            }
            Date dateid = quote.getDateid();
            if (dateid != null) {
                dateid.getQuoteList().remove(quote);
                dateid = em.merge(dateid);
            }
            Stock stock = quote.getStock();
            if (stock != null) {
                stock.getQuoteList().remove(quote);
                stock = em.merge(stock);
            }
            em.remove(quote);
         
    }

    public List<Quote> findQuoteEntities(EntityManager em) {
        return findQuoteEntities(true, -1, -1, em);
    }

    public List<Quote> findQuoteEntities(int maxResults, int firstResult, EntityManager em) {
        return findQuoteEntities(false, maxResults, firstResult, em);
    }

    private List<Quote> findQuoteEntities(boolean all, int maxResults, int firstResult, EntityManager em) {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Quote.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        
    }

    public Quote findQuote(QuotePK id, EntityManager em) {
            return em.find(Quote.class, id);
    }

    public int getQuoteCount(EntityManager em) {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Quote> rt = cq.from(Quote.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
    }
    
}
